package view;

import model.User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class DialogStaffMod extends JDialog {

    private final static int INIT_W = 250;  //窗体初始宽度
    private final static int INIT_H = 300;  //窗体初始高度

    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField tf_id;
    private JTextField tf_name;
    private JTextField tf_pwd;
    private JComboBox<String> cb_jobId;

    SystemPane systemPane;

    public DialogStaffMod(SystemPane systemPane, User oldUser) {
        this.systemPane = systemPane;

        setTitle("修改员工信息");
        setSize(INIT_W,INIT_H);
        setResizable(false);
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        //居中
        Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
        setBounds(p.x - INIT_W / 2, p.y - INIT_H / 2, INIT_W, INIT_H);
        //旧数值
        tf_id.setText(String.valueOf(oldUser.getUserId()));
        tf_name.setText(oldUser.getUserName());
        tf_pwd.setText(oldUser.getPassword());
        cb_jobId.setSelectedIndex(oldUser.getJobId());

        buttonOK.addActionListener(e -> onOK());

        buttonCancel.addActionListener(e -> onCancel());

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(
                e -> onCancel(),
                KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        User newUserInfo = getUser();
        if (newUserInfo != null) {
            if (User.modUser(newUserInfo)) {
                JOptionPane.showMessageDialog(this, "用户修改成功", "修改用户", JOptionPane.INFORMATION_MESSAGE);
                systemPane.notifyUserListChanged();
                dispose();
            }
            else {
                JOptionPane.showMessageDialog(this, "用户修改失败", "修改用户", JOptionPane.ERROR_MESSAGE);
            }
        }
        else {
            JOptionPane.showMessageDialog(this, "用户修改失败，信息不能为空", "修改用户", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    //从输入框获取用户
    User getUser() {
        String str_id = tf_id.getText();
        String str_name = tf_name.getText();
        String str_pwd = tf_pwd.getText();

        if (str_id != null && !str_id.isBlank() && str_name != null && !str_name.isBlank() &&
                str_pwd != null && !str_pwd.isBlank() && cb_jobId.getSelectedIndex() != -1) {
            return new User(
                    Integer.parseInt(str_id),
                    str_name,
                    str_pwd,
                    cb_jobId.getSelectedIndex());
        }

        return null;
    }
}
